package com.lzy.controller;

import com.lzy.base.BaseInfoProperties;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.pojo.ChatMessage;
import com.lzy.pojo.bo.CommentBO;
import com.lzy.pojo.netty.NettyServerNode;
import com.lzy.pojo.vo.CommentVO;
import com.lzy.service.ChatMessageService;
import com.lzy.service.CommentService;
import com.lzy.utils.JsonUtils;
import com.lzy.utils.PagedGridResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@RestController
@RequestMapping("chat")
public class ChatController extends BaseInfoProperties {

    @Resource
    private ChatMessageService chatMessageService;

    @PostMapping("getMyUnReadCounts")
    public GraceJSONResult getMyUnReadCounts(String myId){

        // 获取当前用户的未读消息列表
        Map map = redis.hgetall(CHAT_MSG_LIST + ":" + myId);
        return GraceJSONResult.ok(map);
    }

    @PostMapping("clearMyUnReadCounts")
    public GraceJSONResult clearMyUnReadCounts(String myId,String oppositeId){

        // 获取当前用户的未读消息列表
        redis.setHashValue(CHAT_MSG_LIST + ":" + myId, oppositeId, "0");
        return GraceJSONResult.ok();
    }

    @PostMapping("list/{senderId}/{receiverId}")
    public GraceJSONResult   list(@PathVariable(value = "senderId")  String senderId,
                                  @PathVariable(value = "receiverId") String receiverId,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "20")Integer pageSize){

        PagedGridResult result = chatMessageService.queryChatMsgList(senderId, receiverId, page, pageSize);

        return GraceJSONResult.ok(result);
    }

    @PostMapping("signRead/{msgId}")
    public GraceJSONResult signRead(@PathVariable(value = "msgId") String msgId){
        chatMessageService.updateMsgSignRead(msgId);
        return GraceJSONResult.ok();
    }


    @Resource(name = "curatorClient")
    private CuratorFramework curatorClient;

    @PostMapping("getNettyOnlineInfo")
    public GraceJSONResult getNettyOnlineInfo() throws Exception {

        // 从zookeeper中获取当前已经注册的netty服务列表
        String path = "/server-list"; // 父节点
        List<String> list = curatorClient.getChildren().forPath(path);
        List<NettyServerNode> nettyServerNodes = new ArrayList<>();
        for (String child : list) {
            // 获取子节点的完整路径
            String childPath = path + "/" + child;
            // 获取子节点的数据
            String nodeData = new String(curatorClient.getData().forPath(childPath));
            NettyServerNode nettyServerNode = JsonUtils.jsonToPojo(nodeData, NettyServerNode.class);
            nettyServerNodes.add(nettyServerNode);
        }

        // 计算当前哪个zk的node连接人数是最少的，获得[ip:port]并且返回给前端
        Optional<NettyServerNode> minServerNode = nettyServerNodes.stream()
                .reduce((x, y) -> x.getOnlineCount() < y.getOnlineCount() ? x : y);
        return GraceJSONResult.ok(minServerNode.get());
    }
}